package com.coderknock.bbs.mybatis.domain

import org.springframework.security.core.GrantedAuthority
import org.springframework.security.core.authority.SimpleGrantedAuthority
import org.springframework.security.core.userdetails.UserDetails
import java.io.Serializable
import java.util.*

/**
 * <p></p>
 * @author 三产
 * @version 1.0
 * @date 2017-11-17
 * @QQGroup 213732117
 * @website http://www.coderknock.com
 * @copyright Copyright 2017 拿客 coderknock.com  All rights reserved.
 * @since JDK 1.8
 */
data class User(
        var id: Int = 0,
        var email: String? = null,
        var nickName: String? = null,
        var passWord: String? = null,
        var registerTime: Date = Date(),
        @Transient
        var authorities: MutableList<SimpleGrantedAuthority> = mutableListOf(SimpleGrantedAuthority("ROLE_READER"))
)   //UserDetails 是 Spring Security 用户类的基类 提供相关认证的方法
    : Serializable, UserDetails {
    /**
     * 获取权限，默认有读者权限
     */
    override fun getAuthorities(): MutableCollection<out GrantedAuthority> = authorities

    /**
     * 添加角色
     */
    fun addAuthority(role: String) = authorities.add(SimpleGrantedAuthority("ROLE_$role"))

    override fun isEnabled(): Boolean = true

    override fun getUsername(): String = email!!

    override fun isCredentialsNonExpired(): Boolean = true

    override fun getPassword(): String = passWord!!

    override fun isAccountNonExpired(): Boolean = true

    override fun isAccountNonLocked(): Boolean = true
}